package com.bw.module_video_detail.test_camera

import android.graphics.SurfaceTexture
import android.hardware.Camera
import android.os.Bundle
import android.view.TextureView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.bw.module_video_detail.R
import java.io.IOException

class CameraTextureViewActivity : AppCompatActivity(), TextureView.SurfaceTextureListener {
    private lateinit var textureView: TextureView
    private var camera: Camera? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_camera_texture_view)
        textureView = findViewById(R.id.texture_view)
        textureView.surfaceTextureListener = this
    }

    override fun onSurfaceTextureAvailable(surface: SurfaceTexture, width: Int, height: Int) {
        try {
            camera = Camera.open()
            camera?.setPreviewTexture(surface)
            camera?.setDisplayOrientation(90)
            camera?.startPreview()
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }

    override fun onSurfaceTextureSizeChanged(surface: SurfaceTexture, width: Int, height: Int) {
        // 处理表面尺寸变化
        camera?.apply {
            stopPreview()
            setPreviewTexture(surface)
            startPreview()
        }
    }

    override fun onSurfaceTextureDestroyed(surface: SurfaceTexture): Boolean {
        camera?.apply {
            stopPreview()
            release()
        }
        camera = null
        return true
    }

    override fun onSurfaceTextureUpdated(surface: SurfaceTexture) {

    }
}